package com.keepercloud.qqchat.service.impl;

import com.keepercloud.qqchat.entity.ChatMessage;
import com.keepercloud.qqchat.repository.ChatMessageRepository;
import com.keepercloud.qqchat.service.ChatMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 *
 * @author liyk
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ChatMessageServiceImpl implements ChatMessageService {

    private final ChatMessageRepository chatMessageRepository;

    @Autowired
    public ChatMessageServiceImpl(ChatMessageRepository chatMessageRepository) {
        this.chatMessageRepository = chatMessageRepository;
    }

    @Override
    public <S extends ChatMessage> S save(S entity) {
        return chatMessageRepository.save(entity);
    }

    @Override
    public Long countByFromUsernameAndToUsername(String fromUsername, String toUsername) {
        return chatMessageRepository.countByFromUsernameAndToUsername(fromUsername, toUsername);
    }

    @Override
    public List<Map> findByFromUsernameAndToUsername(String fromUsername, String toUsername, Integer pageStart, Integer pageEnd) {
        return chatMessageRepository.findByFromUsernameAndToUsername(fromUsername, toUsername, pageStart, pageEnd);
    }
}
